package com.example.lib_log

import com.example.lib_log.logger.FileLogger
import com.example.lib_log.logger.LogcatLogger
import com.example.lib_log.logger.NetWorkLogger

/*
构建者模式builder: 参数属性比较多，按需设置
 */
class Logger private constructor(private val TAG:String,private val level: LoggerLevel,private val loggerStrategy: LoggerStrategy,private val isDebug:Boolean,private val saveUrl:String){

    private var logger:ILogger
    init{
        logger=getLogger()
    }
    /*
    策略者模式，根据不同的策略创建不同的对象
     */
    private fun getLogger():ILogger{
         when(loggerStrategy){
            LoggerStrategy.LOGCAT->
               logger= LogcatLogger()

            LoggerStrategy.FILE->{
                logger=FileLogger()
            }
            LoggerStrategy.NET->{
                logger= NetWorkLogger()
            }
        }
        logger.setLoggerLevel(level)
        logger.setDebug(isDebug)
        logger.setLogTAG(TAG)
        logger.setSaveUrl(saveUrl)
        return logger
    }

    class Builder{
        var TAG:String="默认TAG"
        var level:LoggerLevel=LoggerLevel.Debug
        var loggerStrategy:LoggerStrategy=LoggerStrategy.LOGCAT
        var isDebug:Boolean=true
        var saveUrl:String="默认路径"
        fun setTAG(TAG:String):Builder{
            this.TAG=TAG
            return this
        }
        fun setLevel(level:LoggerLevel):Builder{
            this.level=level
            return this
        }
        fun setLoggerStrategy(loggerStrategy:LoggerStrategy):Builder{
            this.loggerStrategy=loggerStrategy
            return this
        }
        fun setDebug(isDebug:Boolean):Builder{
            this.isDebug=isDebug
            return this
        }
        fun setSaveUrl(url:String):Builder{
            this.saveUrl=url
            return this
        }
        fun build():Logger{
            return Logger(this)
        }
    }

    //次构造必须继承主构造
    constructor(builder: Logger.Builder):this(
        builder.TAG,
        builder.level,
        builder.loggerStrategy,
        builder.isDebug,
        builder.saveUrl
    )

     fun d(tag: String, log: String) {
        logger.d(tag,log)
    }

     fun v(tag: String, log: String) {
        logger.v(tag,log)
    }

     fun i(tag: String, log: String) {
        logger.i(tag,log)
    }

     fun w(tag: String, log: String) {
        logger.w(tag,log)
    }

     fun e(tag: String, log: String) {
        logger.e(tag,log)
    }

}